package hw1.package1.space;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CitiesAdapter extends BaseAdapter {

   
		private Activity context;
		ArrayList<City> cities;
	 
		public CitiesAdapter (Activity _context)
		{
			this.context = _context;
			cities = new ArrayList<City>();
			
		}
	 
		public View getView (int pos, View convertView, ViewGroup list) 
		{
			// the method returns the view from the position "pos" from the list
			// convertView is a list element which is no longer visible and can be converted
			
			if(convertView==null){
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = inflater.inflate(R.layout.cities, null);
				}
			
			TextView nume = (TextView)convertView.findViewById(R.id.nume_oras);
			nume.setText(cities.get(pos).nume);

				
			return convertView;
			
		}
	 

	        public int getCount ()
	        {
			// returns the number of elements in the list
	                return cities.size ();
		}
	 
		
		public Object getItem(int pos) 
		{
			// returns the element from the position pos from the model
			return cities.get(pos);
		}
	 
		
		public long getItemId(int position) 
		{
			// fiecare element din lista poate avea un id, nu este insa obligatoriu
			return 0;
		}
		
		public void sortCities(ArrayList <City> cities){  //sorting the city list
			City c=new City();
			boolean b=true;
			if(cities.size()>1)
			while(b){
				b=false;
				for(int i=0;i<cities.size()-1;i++){
					if(cities.get(i).nume.compareTo(cities.get(i+1).nume)>0){
						c=cities.get(i);
						cities.set(i,cities.get(i+1));
						cities.set(i+1, c);
						b=true;
				}
			}
			}
		}
	 
	        public void addCity (String nume)   { //method for adding a new city to the list
	              City c = new City ();
	              c.nume = nume;
	              cities.add(c); //add the city to the list
	              sortCities(cities);  //sort the list
	   
	              // asks the list the refresh its content
	              this.notifyDataSetChanged();
	        }
	        public void removeCity (City c)
	        {
	 
	              cities.remove(c);
	           // asks the list the refresh its content
	              this.notifyDataSetChanged();
	        }

	
}
